package org.csu.mall.utils;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.concurrent.TimeUnit;

//缓存Username
public class UsernameUtil {
    private static Logger logger = LoggerFactory.getLogger(UsernameUtil.class);
    private static LoadingCache<String ,String> localCache =
            CacheBuilder.newBuilder().initialCapacity(1000).maximumSize(5000).expireAfterAccess(1, TimeUnit.HOURS)
                    .build(new CacheLoader<String, String>() {
                        @Override
                        public String load(String s) throws Exception {
                            return null;
                        }
                    });

    public static void setUsername(String key, String value){
        localCache.put(key,value);
    }
    public static void setUserId(String key, Integer value){
        localCache.put(key,String.valueOf(value));
    }
    public static void setOrderNo(String key, BigInteger value){localCache.put(key,String.valueOf(value));}

    public static void setBalance(String key, BigDecimal value){localCache.put(key,String.valueOf(value));}

    public static String getUsername(String key) {
        String Username = null;
        try {
            Username = localCache.get(key);
        } catch (Exception e) {
            logger.error("Guava's LocalCache get error", e);
        }
        return Username;
    }
    public static String getUserId(String key) {
        String Userid = null;
        try {
            Userid = localCache.get(key);
        } catch (Exception e) {
            logger.error("Guava's LocalCache get error", e);
        }
        return Userid;
    }

    public static String getOrderNo(String key) {
        String OrderNo = null;
        try {
            OrderNo = localCache.get(key);
        } catch (Exception e) {
            logger.error("Guava's LocalCache get error", e);
        }
        return OrderNo;
    }
    public static String getBalance(String key) {
        String balance = null;
        try {
            balance = localCache.get(key);
        } catch (Exception e) {
            logger.error("Guava's LocalCache get error", e);
        }
        return balance;
    }



}
